// pages/publish-demand/publish-demand.js
const common = require('../../utils/common.js')
const app = getApp()

Page({
  data: {
    // 表单数据
    formData: {
      title: '',
      category: '',
      description: '',
      budget: '',
      deadline: '',
      urgent: false,
      content: '',
      requirements: '',
      projectDescription: '',
      cooperation: '',
      specialNotice: '',
      budgetNotice: '',
      location: '',
      contactName: '',
      contactPhone: ''
    },
    
    // 分类列表
    categoryList: ['办公场地', '金融贷款', '劳务派遣', '物流代办', '集采代购', '农产代销', '农机服务', '其他'],
    categoryIndex: -1,
    
    // 今天日期
    todayDate: ''
  },

  onLoad(options) {
    // 获取今天日期
    const date = new Date()
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    this.setData({
      todayDate: `${year}-${month}-${day}`
    })
  },

  // 输入框变化
  onInputChange(e) {
    const field = e.currentTarget.dataset.field
    const value = e.detail.value
    this.setData({
      [`formData.${field}`]: value
    })
  },

  // 选择分类
  onCategoryChange(e) {
    const index = e.detail.value
    this.setData({
      categoryIndex: index,
      'formData.category': this.data.categoryList[index]
    })
  },

  // 选择截止日期
  onDeadlineChange(e) {
    this.setData({
      'formData.deadline': e.detail.value
    })
  },

  // 切换紧急状态
  toggleUrgent() {
    this.setData({
      'formData.urgent': !this.data.formData.urgent
    })
  },

  // 预览
  onPreview() {
    // 验证必填字段
    const validation = this.validateForm()
    if (!validation.valid) {
      common.showToast(validation.message, 'none')
      return
    }

    // TODO: 跳转到预览页面
    common.showToast('预览功能开发中...', 'none')
  },

  // 提交
  onSubmit() {
    // 验证必填字段
    const validation = this.validateForm()
    if (!validation.valid) {
      common.showToast(validation.message, 'none')
      return
    }

    common.showLoading('发布中...')
    
    // TODO: 调用接口发布需求
    setTimeout(() => {
      common.hideLoading()
      common.showModal('发布成功', '您的需求已成功发布，等待审核', false).then(() => {
        wx.navigateBack()
      })
    }, 1500)
  },

  // 表单验证
  validateForm() {
    const { title, category, description, budget, deadline, content, location, contactName, contactPhone } = this.data.formData

    if (!title || !title.trim()) {
      return { valid: false, message: '请输入需求标题' }
    }

    if (!category) {
      return { valid: false, message: '请选择需求分类' }
    }

    if (!description || !description.trim()) {
      return { valid: false, message: '请输入需求描述' }
    }

    if (!budget || !budget.trim()) {
      return { valid: false, message: '请输入预算范围' }
    }

    if (!deadline) {
      return { valid: false, message: '请选择需求时效' }
    }

    if (!content || !content.trim()) {
      return { valid: false, message: '请输入需求详细内容' }
    }

    if (!location || !location.trim()) {
      return { valid: false, message: '请输入需求地区' }
    }

    if (!contactName || !contactName.trim()) {
      return { valid: false, message: '请输入联系人' }
    }

    if (!contactPhone || !contactPhone.trim()) {
      return { valid: false, message: '请输入联系电话' }
    }

    if (!/^1[3-9]\d{9}$/.test(contactPhone)) {
      return { valid: false, message: '请输入正确的手机号码' }
    }

    return { valid: true }
  }
})
